package com.chen.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.reggie.common.R;
import com.chen.reggie.dto.SetmealDto;
import com.chen.reggie.entity.Category;
import com.chen.reggie.entity.Setmeal;
import com.chen.reggie.entity.SetmealDish;
import com.chen.reggie.service.CategoryService;
import com.chen.reggie.service.SetmealDishService;
import com.chen.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @GetMapping("page")
    private R<Page> page(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(null != name, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, queryWrapper);
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> dtoRecords = records.stream()
                .map((item) -> {
                    SetmealDto setmealDto = new SetmealDto();
                    BeanUtils.copyProperties(item, setmealDto);

                    Long categoryId = item.getCategoryId();
                    Category category = categoryService.getById(categoryId);
                    if (category != null) {
                        setmealDto.setCategoryName(category.getName());
                    }
                    return setmealDto;
                }).collect(Collectors.toList());
        setmealDtoPage.setRecords(dtoRecords);
        return R.success(setmealDtoPage);
    }

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    private R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDishes(setmealDto);
        return R.success("套餐添加成功");
    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    private R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDishes(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 批量停售，起售套餐
     */

    @PostMapping("status/{type}")
    private R<String> status(@PathVariable Integer type, @RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getStatus, type);
        updateWrapper.in(Setmeal::getId, ids);
        setmealService.update(updateWrapper);
        return R.success("套餐状态修改成功");
    }

    /**
     * 获取套餐信息
     */

    @GetMapping("{id}")
    private R<SetmealDto> getInfo(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> dishList = setmealDishService.list(queryWrapper);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(dishList);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息
     */

    @PutMapping
    private R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //重新插入菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream()
                .map((item) -> {
                    item.setSetmealId(setmealDto.getId());
                    return item;
                }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
        return R.success("套餐信息修改成功");
    }

    /**
     * 根据分类id查询套餐信息
     */
    @GetMapping("list")
    private R<List<Setmeal>> list(Setmeal setmeal) {
//        log.info("categoryId={},status={}",setmeal.getCategoryId(),setmeal.getStatus());
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);

        return R.success(setmealList);

    }

}
